#include <jni.h>
#include <string.h>
#include <malloc.h>
#include <android/log.h>
#define LOG_TAG "cloud_phone"
#define LOGINFO(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGERROR(...)  __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
// Write C++ code here.
//
// Do not forget to dynamically load the C++ library into your application.
//
// For instance,
//
// In MainActivity.java:
//    static {
//       System.loadLibrary("phone");
//    }
//
// Or, in MainActivity.kt:
//    companion object {
//      init {
//         System.loadLibrary("phone")
//      }
//    }
extern "C" JNIEXPORT jint JNICALL

Java_com_cloud_phone_MainActivity_00024Companion_mallocMem(JNIEnv *env, jobject thiz, jint mb) {
    jint total = 0;
    for (jint i = 0; i < mb; i++) {
        jint size = sizeof(char) * 1024 * 1024;
        char *p = (char *) malloc(size);
        if (p != NULL) {
            memset(p, 1, size);
            LOGINFO("malloc success: %d mb\n", mb);
            total += 1;
        } else {
            LOGERROR("malloc failed: %d mb\n");
        }
    }
    LOGINFO("malloc total: %d mb\n", total);
    return total;
}